package com.live.dengdengtv.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.live.dengdengtv.activity.my.MyAccountActivity;
import com.live.dengdengtv.bean.AppConstants;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "WXPayEntryActivity";
	public static final String Key_WXPayBackCode = "WXpay_BackCode";

    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    	api = WXAPIFactory.createWXAPI(this, AppConstants.WX_appid);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG,"onPayFinishCode=" + resp.errCode);
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

			Intent intent = new Intent(this, MyAccountActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.putExtra(Key_WXPayBackCode, resp.errCode);
			startActivity(intent);
			finish();
//			Intent intent = new Intent(AppConstants.WXPAY_CALLBACK);
//			Bundle bundle = new Bundle();
//			resp.toBundle(bundle);
//			bundle.putInt("WXpay_BackCode",resp.errCode);
//			intent.putExtras(bundle);
//			sendBroadcast(intent);
//			finish();
		}
	}
}